package com.wm.blog_common.util;


import com.wm.blog_common.base.BeanUtils;
import com.wm.blog_common.entity.TMenu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/***
 * @ClassName: MenuUtil
 * @Description: 菜单工具类
 * @Author: wm_yu
 * @Create_time: 11:52 2020-3-4
 */
@Slf4j
public class MenuUtil {


    private MenuUtil(){}

    /**
     * 构建菜单树  根据sort顺序排序 针对二级菜单构建,废弃
     * @return
     */
    @Deprecated
    public static List<MenuTree> buildMenuTree(List<TMenu> menuList){
        List<MenuTree> resultMenuList = new ArrayList<>();
        if(CollectionUtils.isEmpty(menuList)){
            return resultMenuList;
        }
        //所有的顶级菜单(一级菜单)
        List<TMenu> firstMenuList = menuList
                .stream()
                .filter(x -> -1 == x.getParentId())
                .collect(Collectors.toList());

        List<TMenu> secondMenuList = menuList
                .stream()
                .filter(x -> -1 != x.getParentId())
                .collect(Collectors.toList());
        //循环所有的一级菜单,找寻旗下的二级菜单
        firstMenuList.forEach(x -> {
            MenuTree menuTree = new MenuTree();
            BeanUtils.copyProperties(x,menuTree);
            List<MenuTree> childrenList = new ArrayList<>();
            //循环子菜单,查询父节点等于上级菜单id的数据
            secondMenuList.forEach(y -> {
                if(x.getId().equals(y.getParentId())){
                    MenuTree secondMenu = new MenuTree();
                    BeanUtils.copyProperties(y,secondMenu);
                    childrenList.add(secondMenu);
                }
            });
            menuTree.setChildren(childrenList);
            resultMenuList.add(menuTree);

        });
       return resultMenuList;
    }


    /**
     * 多级菜单深入构建 根据sort顺序排序
     */
    public static List<MenuTree> buildDeepTree(List<TMenu> menuList){
        List<MenuTree> resultMenuList = new ArrayList<>();
        if(CollectionUtils.isEmpty(menuList)){
            return resultMenuList;
        }
        List<MenuTree> allMenuList = menuList
                .stream()
                .map(x -> {
            MenuTree menuTree = new MenuTree();
            BeanUtils.copyProperties(x, menuTree);
            return menuTree;
        })
                .collect(Collectors.toList());

        //所有的顶级菜单(一级菜单)
        List<MenuTree> firstMenuList = allMenuList
                .stream()
                .filter(x -> -1 == x.getParentId())
                .collect(Collectors.toList());
        firstMenuList.forEach(x -> {
            List<MenuTree> childrenList = getChild(x.getId(), allMenuList);
            List<MenuTree> sortTreeList = sortMenuTree(childrenList);
            x.setChildren(sortTreeList);
        });
        List<MenuTree> sortList = sortMenuTree(firstMenuList);
        return sortList;
    }


    /**
     * 递归查找菜单数据,构建多层树形结构菜单
     * @param id 父级id
     * @param menuList
     * @return
     */
    private static List<MenuTree> getChild(Long id, List<MenuTree> menuList){
        List<MenuTree> childrenList = new ArrayList<>();
        for (MenuTree menu : menuList) {
            //父节点的id和当前循环的父级id相等
            if(id.equals(menu.getParentId())){
                childrenList.add(menu);
            }
        }
        //没有子菜单的结束流程,针对顶级节点肯定不会到这里
        if(CollectionUtils.isEmpty(childrenList)){
            return new ArrayList();
        }
        //递归处理
        for (MenuTree menu : childrenList) {
            //取出menuList中的一级节点,减少数据量
            List<MenuTree> list = menuList.stream().filter(x -> -1 != x.getParentId()).collect(Collectors.toList());
            List<MenuTree> childList = getChild(menu.getId(), list);
            //设置子菜单
            menu.setChildren(childList);
        }
        //排序
        List<MenuTree> sortTree = sortMenuTree(childrenList);
        return sortTree;
    }


    /**
     * 菜单排序 根据sort
     * @param menuTreeList
     * @return
     */
    private static List<MenuTree> sortMenuTree(List<MenuTree> menuTreeList){
        if(CollectionUtils.isEmpty(menuTreeList)){
            return menuTreeList;
        }
       return menuTreeList
               .stream()
               .sorted(Comparator.comparing(MenuTree::getSort))
               .collect(Collectors.toList());
    }


}
